<?php if (! defined('APPPATH')) die('No direct script access allowed');
/**
 * RENARTI IK
 * An open source application development framework for PHP 5.2.0 or newer
 *
 * @package		RtiFramework
 * @author		Nauris Dambis <nauris.dambis@renarti.com>
 * @copyright	Copyright (c) 2012, RENARTI IK.
 * @license		http://renarti.com/#license
 * @link		http://renarti.com
 * @since		Version 1.0
 */

/*
|--------------------------------------------------------------------------
| RENARTI FRAMEWORK MAIN CLASS - RTI
|--------------------------------------------------------------------------
|	
| Rti is a singleton class serving common framework functionalities.
|
| You can access Rti in every class.
|
*/

class Rti
{
	private static $_vars = array();
	
	private static $_objects = array();
	
	private static $_translations = array();
	
	private static $_models_path = array(); 

	
	
	private static function _get_core_object($key, $name)
	{
		if (! isset(self::$_objects[$name]))
		{
			return self::_load('core', self::$_vars['core'][$key], $name);
		}
		return self::$_objects[$name];		
	}  
	
	protected static function _load($directory, $file, $name = '')
	{
		if (isset(self::$_objects[$name]))
		{
			return self::$_objects[$name];
		}
		else if (is_array($file))
		{
			foreach ($file as $filename)
			{
				self::_load($directory, $filename);
			}
		}
		else 
		{
			switch ($directory)
			{
				case 'models':
					if (empty(self::$_models_path))
					{
						self::loadCore('RtiModel');
						self::loadHelper('RtiDirectory');
						self::$_models_path = RtiDirectory::subdir(APPPATH . 'models');
					}
					
					foreach (self::$_models_path as $folder)
					{
						$folders[] = $folder . '/' . $file . '.php';
					}
					
					break;
					
				default :
					$folders = array(
						APPPATH . $directory . '/' . $file . '.php',
						SYSPATH . $directory . '/' . $file . '.php',
					);
										
					break;
			}			
			
			foreach ($folders as $filename)
			{
				if (file_exists($filename))
				{
					$return = include_once $filename;
					
					if (is_array($return))
					{
						self::$_vars[$file] = $return;						
					}					
					
					if (! empty($name))
					{
						$class = basename($filename, '.php');
						
						$object = new $class();
						
						self::$_objects[$name] = $object;
						
						return $object;
					}
					
					return TRUE;
				}
			}
			
			self::showError('Unable to load the requested file:<br/>' . implode('<br/>', $folders));			
		}
	}
	
	protected static function _load_translations($directory, $file)
	{
		if (is_array($file))
		{
			foreach ($file as $filename)
			{
				self::_load_translations($directory, $filename);
			}
		}
		else 
		{
			$folders = array(
				APPPATH . 'languages/' . $directory . '/' . $file . '.php',
				SYSPATH . 'languages/' . $directory . '/' . $file . '.php',
			);
			
			foreach ($folders as $filename)
			{
				if (file_exists($filename))
				{
					$returns = include_once $filename;
					
					if (is_array($returns))
					{
						self::$_translations[$file] = $returns;		
					}

					return TRUE;
				}
			}
			
			self::showError('Unable to load the requested language file:<br/>' . implode('<br/>', $folders));			
		}
	}

	
	
	public static function loadConfig($file)
	{
		return self::_load('config', $file);
	} 
	
	public static function loadCore($file, $name = '')
    {
    	return self::_load('core', $file, $name);
     }

	public static function loadHelper($file, $name = '')
	{
		return self::_load('helpers', $file, $name = '');
	}
	
	public static function loadTranslations($lang, $file)
	{
		return self::_load_translations($lang, $file);
	}

	public static function loadModel($file, $name = '')
	{
		return self::_load('models', $file, $name);
	} 
	
	public static function loadView($view, $data = array(), $return = FALSE)
	{
		$filename = APPPATH . 'views/' . $view . '.php';
		
		if (file_exists($filename))
		{
			if (! empty($data))
			{
				extract($data);
			}

			ob_start();		
			
			include $filename;	

			$buffer = ob_get_clean();
		
			if ($return === TRUE)
			{
				return $buffer;
			}
			else
			{
				echo $buffer;
				return TRUE;
			}
		}
		
		self::showError("Unable load requested view file $filename");		
	} 

	public static function loadController($file, $name = '')
	{
		return self::_load('controllers', $file, $name);
	} 
	
	
	
	/**
	 * @return RtiConfig
	 */
	public static function config($key = '')
	{	
		return self::_get_core_object('CONFIG', 'config')->setSection($key);
	}
	
	/**
	 * @return RtiApplication
	 */
	public static function application()
	{
		return self::_get_core_object('APPLICATION', 'application');
	}

	/**
	 * @return RtiController
	 */
	public static function controller()
	{
		return (isset(self::$_objects['controller']))? self::$_objects['controller'] : NULL;
	}
	
	/**
	 * @return RtiDatabase
	 */
	public static function db($alias = 'default')
	{
		return self::_get_core_object('DATABASE', 'db')->setAlias($alias);
	}
	
	/**
	 * @return RtiTranslator
	 */
	public static function translator($key = '')
	{
		return self::_get_core_object('TRANSLATOR', 'translator')->setSection($key);	
	}
	
	/**
	 * @return RtiURI
	 */
	public static function uri()
	{
		return self::_get_core_object('URI', 'uri');	
	}
	
	/**
	 * @return RtiInput
	 */
	public static function input()
	{
		return self::_get_core_object('INPUT', 'input');	
	}
	
	/**
	 * @return RtiEncoder
	 */
	public static function encoder()
	{
		return self::_get_core_object('ENCODER', 'encoder');	
	}
	
	/**
	 * @return RtiSession
	 */
	public static function session()
	{
		return self::_get_core_object('SESSION', 'session');	
	}
			
	/**
	 * @return RtiCookies
	 */
	public static function cookies()
	{
		return self::_get_core_object('COOKIES', 'cookies');	
	}
	
	/**
	 * @return RtiAgent
	 */
	public static function agent()
	{
		return self::_get_core_object('AGENT', 'agent');	
	}
	
	/**
	 * @return RtiLog
	 */
	public static function log()
	{
		return self::_get_core_object('LOG', 'log');	
	}
	
	/**
	 * @return RtiBenchmark
	 */
	public static function benchmark()
	{
		return self::_get_core_object('BENCHMARK', 'benchmark');	
	}
	
	/**
	 * @return RtiProfiler
	 */
	public static function profiler()
	{
		return self::_get_core_object('PROFILER', 'profiler');	
	}

	
	
	/**
	 * @return array
	 */
	public static function getLoadedVars()	
	{
		return self::$_vars;
	}
	
	/**
	 * @return array
	 */
	public static function getLoadedFiles()
	{
		return get_included_files();
	}

	/**
	 * @return array
	 */
	public static function getLoadedObjects()	
	{
		return self::$_objects;
	}
	
	/**
	 * @return array
	 */
	public static function getTranslations()	
	{
		return self::$_translations;
	}

	
	
	/**
	 * @return void
	 */
	public static function show404()
	{
		exit('Error 404: File not found!');		
	}
	
	/**
	 * @return void
	 */
	public static function showError($message)
	{
		exit($message);
	}

	
	
	/**
	 * @param string $path
	 * @param boolean $path
	 * @return string
	 */
	public static function baseURL($path = '',  $https = FALSE)
	{
		$protocol = ($https === TRUE)? 'https://' : 'http://';
		return $protocol . HTTPHOST . $path;
	}
	
	/**
	 * @param string $path
	 * @param boolean $path
	 * @return string
	 */
	public static function siteURL($path = '', $https = FALSE)
	{
		$url = self::baseURL('', $https);
		
		if (INDEX != '')
		{
			$url.= INDEX . '/';
		}
		
		return $url . $path;
	}
	
	/**
	 * 
	 * @param string $path
	 * @return string
	 */
	public static function basePATH($path = '')
	{
		return (DOCROOT . SUBFOLDER . $path); 
	}

	
	
	/**
	 * 
	 * Enter description here ...
	 * @param string $uri
	 * @param string $method
	 * @param int $http_response_code
	 * @return void
	 */
	public static function redirect($uri = '', $method = 'location', $http_response_code = 302)
	{
		if (! preg_match('#^https?://#i', $uri))
		{
			$uri = self::siteUrl($uri);
		}

		switch($method)
		{
			case 'refresh':
				header("Refresh:0;url=" . $uri);
				break;
				
			default: 
				header("Location: " . $uri, TRUE, $http_response_code);
				break;
		}
		
		exit();		
	}
	
	
	/**
	 * 
	 * Returns RENARTI - MVC based PHP Framework version 
	 * @return string
	 */
	public static function getVersion()
	{
		return "RENARTI - MVC based PHP Framework v.1.0";
	}
}


/* These config files are loaded automatically */
Rti::loadConfig(array('core', 'config', 'autoloads', 'routes'));	


/* End of file Rti.php */
/* Location: ./system/Rti.php */
